package com.lz.kunkun.messageservice.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lz.kunkun.messageservice.GlobalConstant;
import com.lz.kunkun.messageservice.ServicesManager;
import com.lz.kunkun.messageservice.model.BaseResult;
import com.lz.kunkun.messageservice.model.MessageSend;
import com.lz.kunkun.messageservice.model.SysUser;
import com.lz.kunkun.messageservice.service.IMessageSendService;

/**
 * Hello world!
 *
 */
@RestController
@RequestMapping("mes/")
public class MessageSendController {
	//获取信息服务
	public IMessageSendService getMessageSendService() {
		return ServicesManager.instance.getIMessageSendService();
	}
    @GetMapping("sendMes")
    public BaseResult sendMes(HttpServletRequest req,MessageSend mes) {
    	if(null==mes.getPhoneNumber()||mes.getPhoneNumber().equals("")) {
    		return new BaseResult(0,mes.getPhoneNumber()+":无效的号码！");
    	}
    	if(null==mes.getContent()||mes.getContent().equals("")) {
    		return new BaseResult(0,mes.getPhoneNumber()+":无效的内容！");
    	}
    	if(null==mes.getLevel() ||mes.getLevel()>1) {
    		return new BaseResult(0,mes.getPhoneNumber()+":无效的通道类型！");
    	}
    	mes.setUserId(((SysUser)req.getSession().getAttribute("sysUser")).getUserId());
    	int status=getMessageSendService().sendMes(mes); 	
	    return status==0?new BaseResult(status,"系統异常，请联系管理员QQ:"+GlobalConstant.MANAGER_QQ):new BaseResult(status,"发送成功！");
    }
    @GetMapping("allMes")
    public List<MessageSend> allMes(HttpServletRequest req,Integer status,String keyword) {
	    return getMessageSendService().allMes(((SysUser)req.getSession().getAttribute("sysUser")).getUserId(),status,keyword); 	
    }
    @GetMapping("delMes/{mesId}")
    public BaseResult sendMes(HttpServletRequest req,@PathVariable Integer mesId) {
    	SysUser user=((SysUser)req.getSession().getAttribute("sysUser"));
    	MessageSend ms=getMessageSendService().getMes(user.getUserId(),mesId);
    	if(ms==null||ms.getStatus()==1) return new BaseResult(0,"无法删除正在发送的消息，请先取消再操作！");
	    int status=getMessageSendService().delMes(mesId,user.getUserId()); 	
	    return status==0?new BaseResult(status,"系統异常，请联系管理员QQ:"+GlobalConstant.MANAGER_QQ):new BaseResult(status,"删除成功！");
    }
    
    @GetMapping("getMes/{mesId}")
    public MessageSend getMes(HttpServletRequest req,@PathVariable Integer mesId) {
	    return getMessageSendService().getMes(((SysUser)req.getSession().getAttribute("sysUser")).getUserId(),mesId);
    }
    
    @GetMapping("updMes/{mesId}/{status}")
    public BaseResult sendMes(HttpServletRequest req,@PathVariable Integer mesId,@PathVariable Integer status) {
    	SysUser user=((SysUser)req.getSession().getAttribute("sysUser"));
    	MessageSend ms=getMessageSendService().getMes(user.getUserId(),mesId);
    	if(ms==null||ms.getStatus()==2) return new BaseResult(0,"无法操作已发送成功的消息！");
        if(ms.getStatus()==0&&status==1 ) {
        	ms.setStatus(status);
            ms.setWhy("重新发送中");
        }
        else if(ms.getStatus()==1&&status==0 ) {
        	ms.setStatus(status);
        	ms.setWhy("用户取消");
        }else {
        	 return new BaseResult(0,"系統异常，请联系管理员QQ:"+GlobalConstant.MANAGER_QQ);	
        }
	    int status1=getMessageSendService().updMes(ms); 	
	    return status1==0?new BaseResult(status1,"系統异常，请联系管理员QQ:"+GlobalConstant.MANAGER_QQ):new BaseResult(status1,"操作成功！");
    }
}
